package socratesteam.model.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import socratesteam.model.dominio.Estado;
import socratesteam.model.excepcion.DAOExcepcion;
import socratesteam.model.util.ConexionBD;

public class EstadoDAO extends BaseDAO{

	private static final long serialVersionUID = 8063026549927190026L;
	
	private Connection oConnection = null;
	private PreparedStatement oStatement = null;
	private ResultSet oSet = null;
	private String query = "";
	
	public List<Estado> listarEstados(Estado oEstado) throws DAOExcepcion {
		List<Estado> lista = new ArrayList<Estado>();
		Estado o;
		try {
			query = "SELECT T.N_CODIGO, T.C_NOMBRE_CORTO, T.C_NOMBRE_LARGO " +
					"FROM TAB_ESTADO T " +
					"WHERE T.N_CODIGO_TABLA = ? " +
					"AND T.N_ESTADO = 1";
			oConnection = ConexionBD.obtenerConexion();
			oStatement = oConnection.prepareStatement(query);
			oStatement.setInt(1, oEstado.getCodigoTabla());
			oSet = oStatement.executeQuery();
			while (oSet.next()) {
				o = new Estado();
				o.setCodigo(oSet.getInt(1));
				o.setNombreCorto(oSet.getString(2));
				o.setNombreLargo(oSet.getString(3));
				lista.add(o);
			}

		} catch (SQLException e) {
			System.err.println(e.getMessage());
			throw new DAOExcepcion(e.getMessage());
		} finally {
			this.cerrarResultSet(oSet);
			this.cerrarStatement(oStatement);
			this.cerrarConexion(oConnection);
		}
		return lista;
	}
}
